﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BushakwiaRLF
{
    public partial class NewAnalysis : Form
    {
        private RLFClient rlf;
        private Bitmap sourceBitmap;
        private Bitmap originalBitmap;

        public NewAnalysis()
        {
            InitializeComponent();
        }

        private void CreateResults(Bitmap bitmap, int windowSize, int sampleSize, LoadingScreen loadingScreen)
        {
            try
            {
                this.rlf = new RLFClient(bitmap, windowSize, sampleSize);
                this.rlf.CalculateOutputImages();

                // show result form
                this.Invoke(new Action(delegate() { new ResultForm(this.rlf).Show(); }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.StackTrace, "Stack trace", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // close loading window if it isn't already
                if (!loadingScreen.IsDisposed)
                    loadingScreen.Invoke(new Action(delegate() { loadingScreen.Close(); }));

                // enable the main window
                this.Invoke(new Action(delegate() { this.Enabled = true; }));
            }
        }

        private void sourcePreviewPictureBox_Click(object sender, EventArgs e)
        {
            if (openSourceImageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                originalBitmap = new Bitmap(openSourceImageDialog.FileName);
                sourceBitmap = originalBitmap;
                sourceBitmap = new Bitmap(ProcessClass.quantize(ProcessClass.makeGrayscale(originalBitmap), quantizationTrackBar.Value));
                sourcePreviewPictureBox.Image = sourceBitmap;
                windowSizeTrackBar.Enabled = sampleSizeTrackBar.Enabled = quantizationTrackBar.Enabled = startAnalysisButton.Enabled = true;
            }
        }

        private void startAnalysisButton_Click(object sender, EventArgs e)
        {
            if (this.sourceBitmap == null)
                return;

            LoadingScreen loadScreen = new LoadingScreen();
            loadScreen.Show();

            int windowSize = this.windowSizeTrackBar.Value;
            int sampleSize = this.sampleSizeTrackBar.Value;

            Bitmap workingCopy = new Bitmap(this.sourceBitmap);
            Task.Factory.StartNew(() => CreateResults(workingCopy, windowSize, sampleSize, loadScreen));

            //blocking this window until calculations end
            this.Enabled = false;

            //// Use this for debugging.
            //this.rlf = new RLFClient(this.sourceBitmap, this.windowSizeTrackBar.Value, this.sampleSizeTrackBar.Value);
            //this.rlf.CalculateOutputImages();
            //new ResultForm(this.rlf).Show();
        }

        private void quantizationTrackBar_Scroll(object sender, EventArgs e)
        {
            startAnalysisButton.Enabled = false;
            labelQuantization.Text = quantizationTrackBar.Value.ToString();
        }

        private void windowSizeTrackBar_Scroll(object sender, EventArgs e)
        {
            labelWindowSize.Text = windowSizeTrackBar.Value.ToString();
        }

        private void sampleSizeTrackBar_Scroll(object sender, EventArgs e)
        {

            labelSampleSize.Text = sampleSizeTrackBar.Value.ToString();
        }

        private void quantizationTrackBar_MouseUp(object sender, MouseEventArgs e)
        {
            sourcePreviewPictureBox.Image = sourceBitmap = new Bitmap(ProcessClass.quantize(ProcessClass.makeGrayscale(originalBitmap), quantizationTrackBar.Value));
            startAnalysisButton.Enabled = true;
        }
    }
}
